﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Model.Param.YiDongYuan.Hzx;
using YiSha.Util;
using YiSha.Util.Extension;

namespace YiSha.Service.YiDongYuan
{
    public class Upload2Hzx
    {
        private static string appId = "1108334916531135664";
        private static string userName = "ddyjc";
        private static string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MzA2NjI5NTcsInN1YiI6IntcImFwcElkXCI6XCIxMTA4MzM0OTE2NTMxMTM1NjY0XCIsXCJ1c2VyTmFtZVwiOlwiZGZmZjZkZWRlMWQ3MTQ3ZWNlYzI4NjE4MTc2ZTBhNGRjMzU2NGIyODU1MzQ2ZTM5MjcyNWZmMmRlNWY3NzE5MVwifSIsImF1ZCI6IjI1MWI2ZWQ5OWRhZTQwY2M3M2Q1ZmYwMmY4OWQ3MDFjNzExMTFiZjAzZGYwMjA4OTQ0MTJkNDM2MmNlZjBiOGEiLCJleHAiOjE2OTI4NzA5NTcsIm5iZiI6MTYzMDY2Mjk1N30.dBzoVyM1HUmwKKXFnV2uu5ah3P8nedQWyZZ9SDaWls4";
        private static string ReservationUrl = "http://60.222.220.252:8089/sxyc-web/api/reservation";
        private static string SaveTestResultUrl = "http://60.222.220.252:8089/sxyc-web/api/saveTestResult";
        private static string mechanicReceiveUrl = "http://60.222.220.252:8089/sxyc-web/api/centry/ycgps/mechanicReceive";
        private static string uploadImgUrl = "http://60.222.220.252:8089/sxyc-web/api/centry/ycgps/uploadImg";
        private static string UploadDir = @"D://wwwroot/jxhfd-api";

        #region 上传非道检测信息

        private string urlParam = $"appid={appId}&username={userName}&token={token}";

        public string UploadTask(Hzx_Reservation model)
        {
            string url = ReservationUrl;
            string param = model.ToJson();
            LogHelper.Info("上传华志信待检数据(Reservation)：" + param);
            string result = HttpHelper.HttpPost($"{url}?{urlParam}", param, "application/json");
            string id = result.ToJObject().GetValue("msg").ToString();
            if (!id.IsLong())
            {
                LogHelper.Info($"上传华志信待检数据(Reservation)出错{model.codeNum}：" + id);
            }
            else
            {
                LogHelper.Info($"上传华志信待检数据(Reservation)成功 {model.codeNum}");
            }
            return id;
        }

        public string UploadTestResult(Hzx_TestResult model)
        {
            string url = SaveTestResultUrl;
            model.videoId = UploadImg(model.videoId);
            string param = model.ToJson();
            LogHelper.Info("上传华志信检测数据(SaveTestResult)：" + param);
            string result = HttpHelper.HttpPost($"{url}?{urlParam}", param, "application/json");
            LogHelper.Info($"上传华志信检测结果SaveTestResult {model.testId}，结果是：{result}");
            return result;
        }

        #endregion 上传非道检测信息

        #region 上传非道机械信息

        public string UploadMechanics(DetectionMechanicsEntity model)
        {
            try
            {
                string param = "{";
                param += $"\"userType\":\"0\"," +
                    $"\"subjectType\":\"1\",";
                param += $"\"ownerName\":\"{model.OwnerName}\"," +
                    $"\"ownerIdcard\":\"{model.OwnerCardNo}\"," +
                    $"\"contactPhone\":\"{model.OwnerTel}\"," +
                    $"\"province\":\"{GetCounty("山西省")}\"," +
                    $"\"city\":\"{GetCounty("运城市")}\"," +
                    $"\"county\":\"{GetCounty(model.Region)}\"," +
                    $"\"status\":\"2\"," +
                    $"\"jixieFlag\":\"0\"," +
                    $"\"mechanictype\":\"{GetMechanicsType(model.JXDJLX)}\"," +
                    $"\"mechanicname\":\"{GetMechanicsName(model.JXLB)}\"," +
                    $"\"enterprise\":\"{model.JXZZQY}\"," +
                    $"\"model\":\"{model.JXXH}\"," +
                    $"\"productdate\":\"{model.JXCCRQ.ParseToDateTime():yyyy-MM-dd}\"," +
                    $"\"codetype\":\"0\",\"codenum\":\"{model.JXCCBH}\"," +
                    $"\"hbxxgkh\":\"{model.HBXXGKBH}\"," +
                    $"\"hbdm\":\"{model.JXHBDM}\"," +
                    $"\"yardNum\":\"{model.YYHP}\"," +
                    $"\"province1\":\"{GetCounty("山西省")}\"," +
                    $"\"city1\":\"{GetCounty("运城市")}\"," +
                    $"\"county1\":\"{GetCounty(model.Region)}\"," +
                    $"\"greenNumber\":\"{model.HuanBaoCode}\"," +
                    $"\"fuekType\":\"{GetRLLX(model.RLLX)}\"," +
                    $"\"enginetype\":\"1\",\"hostcode\":\"{model.FDJBH}\"," +
                    $"\"engModel\":\"{model.FDJXH}\"," +
                    $"\"xshzh\":\"{model.FDJXSHZZH}\"," +
                    $"\"hostpower\":\"{model.FDJGL}\"," +
                    $"\"hostspeed\":\"{model.FDJZS}\"," +
                    $"\"hostenterprise\":\"{model.FDJZZQY}\"," +
                    $"\"hostdate\":\"{model.FDJCCRQ.ParseToDateTime():yyyy-MM-dd}\"," +
                    $"\"hostdischarge\":\"{model.PFJD}\"," +
               $"\"terminalsn\":\"{model.GpsNo}\"," +
               $"\"isOnline\":\"{(model.GpsNo.IsNullOrEmpty() ? "1" : "0")}\",";
                //上传照片
                if (!model.OwnerCard_Pic.IsNullOrEmpty())
                    param += $"\"photo1\":\"{UploadImg(model.OwnerCard_Pic)}\",";

                if (!model.JX1_pic.IsNullOrEmpty())
                    param += $"\"mechanicphoto1\":\"{UploadImg(model.JX1_pic)}\",";
                if (!model.JX2_pic.IsNullOrEmpty())
                    param += $"\"mechanicphoto2\":\"{UploadImg(model.JX2_pic)}\",";
                if (!model.JX3_pic.IsNullOrEmpty())
                    param += $"\"mechanicphoto3\":\"{UploadImg(model.JX3_pic)}\",";
                if (!model.JXMP_pic.IsNullOrEmpty())
                    param += $"\"mechanicnameplate\":\"{UploadImg(model.JXMP_pic)}\",";
                if (!model.FDJMP_pic.IsNullOrEmpty())
                    param += $"\"enginenameplate\":\"{UploadImg(model.FDJMP_pic)}\",";
                if (!model.JXHBDM_pic.IsNullOrEmpty())
                    param += $"\"greencode\":\"{UploadImg(model.JXHBDM_pic)}\",";
                if (!model.HBBQ_pic.IsNullOrEmpty())
                    param += $"\"greenlabel\":\"{UploadImg(model.HBBQ_pic)}\",";
                param += "\"test\":\"test\"}";
                //
                LogHelper.Info("Upload2Hzx-" + model.HuanBaoCode + "：" + param);
                string url = $"{mechanicReceiveUrl}?{urlParam}";
                string result = HttpHelper.HttpPost(url, param, "application/json");
                LogHelper.Info("Upload2HzxResult-" + model.HBBQ_pic + "：" + result);
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return ex.Message;
            }
        }

        private string UploadImg(string id)
        {
            if (id.IsNullOrZero())
            {
                return "";
            }
            var service = new BaseFileService();
            var path = service.GetEntity(id.ParseToLong()).Result.FilePath;
            path = UploadDir + path;// @"C:\AppInfoSystem\"
            if (!File.Exists(path))
            {
                return "";
            }
            string result = HttpHelper.PostUpload($"{uploadImgUrl}?{urlParam}", null, File.ReadAllBytes(path), new FileInfo(path).Name);
            return result.ToJObject().GetValue("fileUrl").ToString();
        }

        private int GetCounty(string region)
        {
            return (int)(RegionCode)System.Enum.Parse(typeof(RegionCode), region);
        }

        private string GetMechanicsType(string mechanics_type)
        {
            switch (mechanics_type)
            {
                case "工程机械":
                    return "0";

                case "场内车辆":
                    return "2";

                case "农业机械":
                    return "5";

                case "固定源":
                    return "8";

                case "电动机械":
                    return "9";

                case "其它":
                    return "10";

                default:
                    return mechanics_type;
            }
        }

        public static string GetMechanicsName(string mechanicsName)
        {
            if (string.IsNullOrEmpty(mechanicsName))
            {
                return "14";
            }
            else if (mechanicsName.Contains("挖掘机"))
            {
                return "3";
            }
            else if (mechanicsName.Contains("推土机"))
            {
                return "6";
            }
            else if (mechanicsName.Contains("装载机"))
            {
                return "2";
            }
            else if (mechanicsName.Contains("叉车"))
            {
                return "5";
            }
            else if (mechanicsName.Contains("压路机"))
            {
                return "1";
            }
            else if (mechanicsName.Contains("摊铺机"))
            {
                return "9";
            }
            else if (mechanicsName.Contains("平地机"))
            {
                return "10";
            }
            else if (mechanicsName.Contains("牵引车"))
            {
                return "13";
            }
            else if (mechanicsName.Contains("堆高机"))
            {
                return "12";
            }
            else if (mechanicsName.Contains("桩工机械"))
            {
                return "11";
            }
            else
            {
                return "14";
            }
        }

        public static string GetRLLX(string rllx)
        {
            switch (rllx)
            {
                case "柴油":
                    return "0";

                case "汽油":
                    return "1";

                case "电动":
                    return "5";

                case "LPG":
                    return "2";

                case "天然气":
                    return "3";

                default:
                    return rllx;
            }
        }

        #endregion 上传非道机械信息
    }

    public enum RegionCode
    {
        山西省 = 140000,
        运城市 = 140800,
        盐湖区 = 140802,
        临猗县 = 140821,
        万荣县 = 140822,
        闻喜县 = 140823,
        稷山县 = 140824,
        新绛县 = 140825,
        绛县 = 140826,
        垣曲县 = 140827,
        夏县 = 140828,
        平陆县 = 140829,
        芮城县 = 140830,
        永济市 = 140881,
        河津市 = 140882,
        经济开发区 = 140883,
        风陵渡开发区 = 140884,
        绛县开发区 = 140885
    }
}
